using System;
using System.Windows;
using System.Windows.Input;
using ResourceBlender.Express.Properties;

namespace ResourceBlender.Express {
    /// <summary>
    /// A window for changing application settings
    /// </summary>
    public partial class WinOptions : Window {
        /// <summary>
        /// Initializes a new <see cref="WinOptions"/>.
        /// </summary>
        public WinOptions() {
            InitializeComponent();
            cboLanguage.ItemsSource = WPFConstants.Languages;
            if(Settings.Default.DBPath.Length == 0) {
                Settings.Default.DBPath = System.IO.Path.Combine(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ResourceBlender"), "ResourceBlender.db3");
            }
        }
        /// <summary>
        /// Validates user input then returns a DialogResult of true.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        public void SaveSettings(object sender, ExecutedRoutedEventArgs e) {
            if(databaseSelector.ValidateSettings()) {
                Settings.Default.FirstRun = false;
                Settings.Default.Save();
                this.DialogResult = true;
                Close();
            }
        }

        /// <summary>
        /// Shows a prompt before clearing the settings and restarting the application.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.ExecutedRoutedEventArgs"/> instance containing the event data.</param>
        private void ClearSettings(object sender, ExecutedRoutedEventArgs e) {
            if(DialogBox.Show(this, "This will clear all settings before closing the application. When you restart ResourceBlender Express, you will be prompted to setup the application again.\n\nThe database will be unaffected.", "Clear Settings", DialogBox.DialogBoxType.YesNo, DialogBox.DialogBoxIcon.Warning, DialogBox.DialogBoxButton.No) == DialogBox.DialogBoxButton.Yes) {
                Settings.Default.Reset();
                Settings.Default.Save();
                Application.Current.Shutdown();
            }
        }
    }
}
